package com.ctg.behavior.calc.impala.transfer.userbehavior;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.ctg.behavior.calc.common.CtSQLParam;
import com.ctg.behavior.calc.impala.report.userbehavior.CtUBReport;
import com.ctg.behavior.calc.impala.transfer.CtFilterTransfer;
import com.ctg.behavior.calc.utils.CtCalcSQLUtils;
import com.ctg.behavior.common.calc.CtCalc;
import com.ctg.behavior.common.calc.CtCalcMultiMeasure;
import com.ctg.behavior.common.consts.CtCalcConsts;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Component
public class CtUBTransfer extends CtFilterTransfer {

	@Override
	public String doTransfer(CtCalc calcObject, CtSQLParam sqlParam) {
		if (!(calcObject instanceof CtUBReport)) {
			log.warn("calcObject is not CtUBReport, class={}", calcObject.getClass());
			log.warn("calcObject: {}", JSON.toJSON(calcObject));
			log.warn("calcObject-sqlParam: {}", JSON.toJSON(sqlParam));
			return "";
		}
		CtUBReport report = (CtUBReport) calcObject;
		log.info("report={}", JSON.toJSONString(report));

		StringBuilder impalaSQL = new StringBuilder();

		// 1. 据LoadType决定SELECT部分字段
		if (CtCalcConsts.LOAD_TYPE_EVENT_TOTAL.equalsIgnoreCase(report.getLoadType())) {
			impalaSQL.append(" SELECT count(*) AS total ");
		} else if (CtCalcConsts.LOAD_TYPE_EVENT_GROUPBY.equalsIgnoreCase(report.getLoadType())) {
			impalaSQL.append(" SELECT event AS event, count(*) AS total ");
		}
		// 2. 表及表关联
		impalaSQL.append(" FROM a_event_parquet_").append(report.getAppId());
		impalaSQL.append(" WHERE 1=1 ");
		// 3. deviceId 限制
		if(!StringUtils.isEmpty(report.getDeviceId())) {
			impalaSQL.append(" AND device_id = '").append(report.getDeviceId()).append("' ");
		}
		// 4. measures的事件限制
		List<CtCalcMultiMeasure> measures = report.getMeasures();
		if (!measures.isEmpty()) {
			CtCalcMultiMeasure theFirstMeausre = measures.get(0);
			List<String> measureEvents = theFirstMeausre.getEvents();
			if (!measureEvents.isEmpty()) {
				impalaSQL.append(" AND ( ");
				for (int index = 0; index < measureEvents.size(); index++) {
					if (index > 0) {
						impalaSQL.append(" OR ");
					}
					impalaSQL.append(" event = '").append(measureEvents.get(index)).append("' ");
				}
				impalaSQL.append(" ) ");
			}
		}
		// 5. filter 限制条件 (userId & eventId 限制)
		String conditionsSQL = super.doTransfer(report.getFilter(), sqlParam);
		if (!CtCalcSQLUtils.isEmptyCondtions(conditionsSQL)) {
			impalaSQL.append(" AND ").append(conditionsSQL);
		}
		// 6. fromDate & toDate 限制条件
		impalaSQL.append(" AND event_date >= '").append(report.getFromDate()).append("' ");
		impalaSQL.append(" AND event_date <= '").append(report.getToDate()).append("' ");
		// 7. 时间戳(按unit参数划分的时间戳)
		impalaSQL.append(" AND event_time >= '").append(report.getCalcDate().getFromDate().getTime()).append("' ");
		impalaSQL.append(" AND event_time <= '").append(report.getCalcDate().getToDate().getTime()).append("' ");
		// 8. GROUP BY (只有 按事件分组维度统计 时， loadType=CtCalcConsts.LOAD_TYPE_EVENT_GROUPBY)
		if (CtCalcConsts.LOAD_TYPE_EVENT_GROUPBY.equalsIgnoreCase(report.getLoadType())) {
			impalaSQL.append(" GROUP BY event ");
		}

		return impalaSQL.toString();
	}

}
